﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DHYHN.ASPNET.HoiNghi.BIZ;
using DHYHN.ASPNET.HoiNghi.DAL;

namespace DHYHN.ASPNET.HoiNghi.Admin
{
    public partial class EditInformation1 : System.Web.UI.Page
    {
        private bool create;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
                return;
            if (Request.QueryString.Count != 2 || Request.QueryString["id"] == null || Request.QueryString["lang"]==null)
                Response.Redirect("~/Error.aspx");
            LoadData();

        }
        private void LoadData()
        {
            LblMsg.Text = "";
            try
            {
                var id = Request.QueryString["id"];
                var lang = Request.QueryString["lang"];
                if (id.Equals("new"))
                {
                    create = true;
                    Page.Title = "Create new information";
                    lblTitle.Text = "Create new information";
                }
                else
                {
                    create = false;
                    InformationHelper helper = new InformationHelper();
                    var info = helper.GetInformation(int.Parse(id),lang);
                    if (info == null)
                        Response.Redirect("~/Error.aspx");
                    BtnDelete.Enabled = info.IsNullable;
                    TxtTitle.Text = info.Title;
                    FckContent.Value = info.Body;

                    Page.Title = "Edit ";
                    lblTitle.Text = "Edit ";
                }
            }
            catch (Exception exception)
            {

                LblMsg.Text = exception.Message;
            }
            BtnDelete.Visible = !create;
            BtnCreate.Visible = create;
            BtnSave.Visible = !create;

            lblTitleEn.Visible = create;
            lblTitleFn.Visible = create;
            lblTitleVn.Visible = create;

            TxtTitleFn.Visible = create;
            TxtTitleVn.Visible = create;

            contentEn.Visible = create;
            contentFn.Visible = create;
            contentVn.Visible = create;

            FckContentFn.Visible = create;
            FckContentVn.Visible = create;

        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            LblMsg.Text = "";
            try
            {
                Information info = new Information();
                info.Id = int.Parse(Request.QueryString["id"]);
                info.Title = TxtTitle.Text;
                info.Body = FckContent.Value;
                info.Lang = Request.QueryString["lang"];
                InformationHelper helper = new InformationHelper();
                helper.SaveInformation(info);
                LblMsg.Text = Message.SaveSuccess[Request.QueryString["lang"]].ToString();
            }
            catch (Exception exception)
            {

                LblMsg.Text = exception.Message;
            }


        }

        protected void BtnDelete_Click(object sender, EventArgs e)
        {
            LblMsg.Text = "";
            try
            {
                InformationHelper helper = new InformationHelper();
                var info = new Information();
                info.Id = int.Parse(Request.QueryString["id"]);
                helper.DeleteInformation(info);
                LblMsg.Text = Message.DeleteSuccess[Request.QueryString["lang"]].ToString();
                TxtTitle.Text = "";
                FckContent.Value = "";
            }
            catch (Exception exception)
            {
                LblMsg.Text=exception.Message;
            }
        }

        protected void BtnCreate_Click(object sender, EventArgs e)
        {
            LblMsg.Text = "";
            try
            {
                InformationHelper helper = new InformationHelper();
                var infoEn = new Information();
                var infoFn = new Information();
                var infoVn = new Information();

                infoEn.IsNullable =infoFn.IsNullable=infoVn.IsNullable=true;
                infoEn.Lang = "en";
                infoFn.Lang = "fn";
                infoVn.Lang = "vn";
                infoEn.Title = TxtTitle.Text;
                infoFn.Title = TxtTitleFn.Text;
                infoVn.Title = TxtTitleVn.Text;

                infoEn.Body = FckContent.Value;
                infoFn.Body = FckContentFn.Value;
                infoVn.Body = FckContentVn.Value;

                helper.CreateInformation(infoEn,infoFn,infoVn);

                LblMsg.Text = Message.CreateSuccess[Request.QueryString["lang"]].ToString();
            }
            catch (Exception exception)
            {
                LblMsg.Text = exception.Message;
            }
        }
    }
}
